#include "view_sports.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "Motor.h"
#include "view.h"
#include "times/view_times.h"
#include "sports_mode.h"
#include "app_trace.h"
#include "view_sports.inc"
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)
void view_sports_base_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
    }
    int x = 52;
    int y = 30;
    img_gif_com_t const img_cfg = {
        .gif_count = 4,
        .img[0].x = x,
        .img[0].y = y,
        .img[0].sn = FILE_IMG_C160_exercise_icon_1_52_30,
        .img[1].x = x,
        .img[1].y = y,
        .img[1].sn = FILE_IMG_C161_exercise_icon_2_52_30,
        .img[2].x = x,
        .img[2].y = y,
        .img[2].sn = FILE_IMG_C162_exercise_icon_3_52_30,
        .img[3].x = x,
        .img[3].y = y,
        .img[3].sn = FILE_IMG_C163_exercise_icon_4_52_30,
    };
    _disp_show_flash_img_gif_all(&img_cfg, DISP_LFS_FILE_INDEX_0);
    // x = 94;
    // y = 155;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_C164_exercise_text_94_155);
    disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_Sport, 50);
}

view_event_result_t view_sports_level_1_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
    {
        if (view == view_sports_level_1_begin)
        {
            display_show(view_sports_level_1_end - 1, DISP_MODE_UP_MOVE);
            return view_event_result_handled;
        }
        return view_event_result_not_handled;
    }
#endif //!< EVENT_PREVIOUS_VIEW
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
    {
        if (view == view_sports_level_1_end - 1)
        {
            display_show(view_sports_level_1_begin, DISP_MODE_DOWN_MOVE);
            return view_event_result_handled;
        }
        return view_event_result_not_handled;
    }
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
    {
        display_show(view_sports_base, DISP_MODE_UP_MOVE);
        return view_event_result_handled;
    }
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
    {
        sports_mode_mode_t mode = sports_mode_mode_invalid;
        switch (view)
        {
        case view_sports_runing:
            mode = sports_mode_mode_run_2;
            break;
        case view_sports_walking:
            mode = sports_mode_mode_walk;
            break;
        case view_sports_cycling:
            mode = sports_mode_mode_ride;
            break;
        case view_sports_fitness:
            mode = sports_mode_mode_work_out;
            break;
        case view_sports_swing:
            mode = sports_mode_mode_swing;
            break;
        default:
            break;
        }
        sports_mode_start(mode);
        display_show(view_sports_state_0, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    }
    return view_event_result_not_handled;
}
#define POS_WIDTH 6
#define POS_INTERVAL 7
void view_sports_level_1_display(int view)
{
    int icon_index = 0;
    localize_string_key_t text_index = (localize_string_key_t)0;
    // localize_string_key_t text_x = (localize_string_key_t)0;
    int pos_index = 0;
    switch (view)
    {
    case view_sports_runing:
        icon_index = FILE_IMG_C101_execries_run_65_27;
        text_index = localize_string_key_SportRun;
        // text_x = 103;
        pos_index = 0;
        break;
    case view_sports_walking:
        icon_index = FILE_IMG_C102_execries_walk_65_27;
        text_index = localize_string_key_SportWalk;
        // text_x = 95;
        pos_index = 1;
        break;
    case view_sports_cycling:
        icon_index = FILE_IMG_C103_execries_cycling_65_27;
        text_index = localize_string_key_SportCycling;
        // text_x = 86;
        pos_index = 2;
        break;
    case view_sports_fitness:
        icon_index = FILE_IMG_C104_execries_fitness_65_27;
        text_index = localize_string_key_SportFitness;
        // text_x = 86;
        pos_index = 3;
        break;
    case view_sports_swing:
        icon_index = FILE_IMG_C105_execries_swing_65_27;
        text_index = localize_string_key_SportSwing;
        // text_x = 92;
        pos_index = 4;
        break;
    default:
        return;
    }
    // sports 5 , sports+back 6
    int const sports_count = view_sports_level_1_end - view_sports_level_1_begin;
#ifdef EVENT_NEXT_VIEW
    int const show_count = sports_count;
#else
    int const show_count = sports_count + 1;
#endif
    int x = 65;
    int y = 27;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, icon_index);
    // x = text_x;
    // y = 159;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, text_index);
    disp_show_localize(DISP_LFS_FILE_INDEX_1, text_index, 50);
    x = (240 - POS_WIDTH * show_count - POS_INTERVAL * (show_count - 1)) / 2; // 105
    // (240 - 6 * 5 - 7 * 4) / 2
    // x = 84;
    y = 193;
    uint16_t color = 0;
    for (int i = 0; i < show_count; i++)
    {
        if (i == pos_index)
        {
            color = RGB565_COLOR_WHITE;
        }
        else
        {
            color = 0x4a49;
        }
        disp_draw_user_image_gray_lucency(x + i * (POS_INTERVAL + POS_WIDTH), y, color, bmp_level_1_point);
    }
}
static bool is_level_1_view(int view)
{
    if (view >= view_sports_level_1_begin && view < view_sports_level_1_end)
    {
        return true;
    }
    else
    {
        return false;
    }
}
void view_sports_level_1_back_display(void)
{
    int x = 68;
    int y = 27;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_C165_back_68_27);
    // x = (240 - 46) / 2;
    // y = 159;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_other_back_text);
    disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_sportBack, 50);

    int const count = (view_sports_level_1_end - view_sports_level_1_begin) + 1;
    int const pos_index = (view_sports_level_1_end - view_sports_level_1_begin);
    uint16_t color = 0;
    // x = (240 - POS_WIDTH * count - POS_INTERVAL * (count - 1)) / 2; // 105
    x = 84;
    y = 193;
    for (int i = 0; i < count; i++)
    {
        if (i == pos_index)
        {
            color = RGB565_COLOR_WHITE;
        }
        else
        {
            color = 0x4a49;
        }
        disp_draw_user_image_gray_lucency(x + i * (POS_INTERVAL + POS_WIDTH), y, color, bmp_level_1_point);
    }
}
void view_sports_prompt_end_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 3000;
    }
    if (disp_ctrl.auto_refresh_count == 0)
    {
        int x = 81;
        int y = 37;
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_C138_endofexercise_81_37);
        // x = 34;
        // y = 159;
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_C139_endofexercise_text_34_159);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_AlertSportEnd, 50);
    }
    else
    {
        if (disp_ctrl.loop_cnt == 0)
        {
            display_show(view_sports_result_0, DISP_MODE_NORMAL);
        }
    }
}
void view_sports_error_short_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 3000;
    }
    if (disp_ctrl.auto_refresh_count == 0)
    {
        // int x = 26;
        // int y = 66;
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_C140_exercise_short_26_66);
        disp_show_localize(DISP_LFS_FILE_INDEX_0, localize_string_key_AlertSportNotSave, 50);
    }
    else
    {
        if (disp_ctrl.loop_cnt == 0)
        {
            display_show(view_sports, DISP_MODE_NORMAL);
        }
    }
}
void view_sports_result_common_diplay(void)
{
    int sports_icon = 0;
    switch (disp_ctrl.com_data.i_temp[0])
    {
    case sports_mode_mode_run_2:
        sports_icon = FILE_IMG_C142_exercise_end_run_96_0;
        break;
    case sports_mode_mode_walk:
        sports_icon = FILE_IMG_C143_exercise_end_walk_96_0;
        break;
    case sports_mode_mode_work_out:
        sports_icon = FILE_IMG_C145_exercise_end_fitness_96_0;
        break;
    case sports_mode_mode_swing:
        sports_icon = FILE_IMG_C146_exercise_end_swing_96_0;
        break;
    case sports_mode_mode_ride:
        sports_icon = FILE_IMG_C144_exercise_end_cycling_96_0;
        break;
    }
    int x = 96;
    int y = 0;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, sports_icon);
    datetime_t date_time = utc_time_to_date_time(disp_ctrl.com_data.i_temp[1]);
    int year = date_time.date.year + 2000;
    int month = date_time.date.month;
    int day = date_time.date.day;
    int hour = date_time.time.hour;
    int minute = date_time.time.minute;
    x = 69;
    y = 51;
    x = disp_draw_user_image_num_with_digits(x, y, year, 4, 8, num_6x14_TAB);
    disp_draw_user_image(x, y, num_6x14_slash);
    x += 7;
    x = disp_draw_user_image_num_with_digits(x, y, month, 2, 8, num_6x14_TAB);
    disp_draw_user_image(x, y, num_6x14_slash);
    x += 7;
    x = disp_draw_user_image_num_with_digits(x, y, day, 2, 8, num_6x14_TAB);
    x += 8;
    x = disp_draw_user_image_num_with_digits(x, y, hour, 2, 8, num_6x14_TAB);
    disp_draw_user_image(x, y, num_6x14_colon);
    x += 4;
    x = disp_draw_user_image_num_with_digits(x, y, minute, 2, 8, num_6x14_TAB);
}
void view_sports_result_1_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = sports_mode_get_now_mode();
        disp_ctrl.com_data.i_temp[1] = sports_mode_get_sports_start_time();
        disp_ctrl.com_data.i_temp[2] = sports_mode_get_sports_mileage();
        disp_ctrl.com_data.i_temp[3] = sports_mode_get_sports_steps_freq();
        disp_ctrl.com_data.i_temp[4] = sports_mode_get_avg_speed() * 360 / 1000; // km/h的10倍
        if (system_config_get_km_mi() != 0)
        {
            disp_ctrl.com_data.i_temp[2] *= 0.6213712;
        }
    }
    switch (disp_ctrl.com_data.i_temp[0])
    {
    case sports_mode_mode_run_2:
    case sports_mode_mode_walk:
        break;
    default:
        return;
    }
    view_sports_result_common_diplay();
    // int const font_width = 9;
    int const num_width = 11;
    int const num_height = 22;
    int const unit_interver = 6;

    // distance
    int x = 61;
    int y = 94;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_C131_distance_icon);
    x = 96;
    y -= 3;
    x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[2] / 1000, num_width, num_9x22_TAB);
    disp_draw_rect_color(x, y + num_height - 3, x + 2, y + num_height - 3 + 1, RGB565_COLOR_WHITE);
    x += 5;
    x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[2] % 1000 / 100, num_width, num_9x22_TAB);
    x += unit_interver;
    disp_draw_user_image(x, y + num_height - 14, bmp_unit_km);

    // setp
    x = 61;
    y += 43;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_C128_step_icon);
    x = 96;
    y -= 3;
    x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[3], num_width, num_9x22_TAB);
    x += unit_interver;
    disp_draw_user_image(x, y + num_height - 15, bmp_unit_step_m);

    // speed
    x = 61;
    y += 43;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, FILE_IMG_C127_speed_icon);
    x = 96;
    y -= 3;
    x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[4] / 10, num_width, num_9x22_TAB);
    disp_draw_rect_color(x, y + num_height - 3, x + 2, y + num_height - 3 + 1, RGB565_COLOR_WHITE);
    x += 5;
    x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[4] % 10, num_width, num_9x22_TAB);
    x += unit_interver;
    disp_draw_user_image(x, y + num_height - 15, bmp_unit_km_h);
}
void view_sports_result_0_display(void)
{

    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = sports_mode_get_now_mode();
        disp_ctrl.com_data.i_temp[1] = sports_mode_get_sports_start_time();
        disp_ctrl.com_data.i_temp[2] = sports_mode_get_sports_duration();
        disp_ctrl.com_data.i_temp[3] = sports_mode_get_sports_maximum_heart_rate();
        disp_ctrl.com_data.i_temp[4] = sports_mode_get_sports_minimal_heart_rate();
        disp_ctrl.com_data.i_temp[5] = sports_mode_get_sports_calorie() / 1000;
        if (system_config_get_km_mi() != 0)
        {
            disp_ctrl.com_data.i_temp[6] *= 0.6213712;
        }
    }

    view_sports_result_common_diplay();
    int x = 61;
    int y = 94;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_C132_time_icon);

    int hour = disp_ctrl.com_data.i_temp[2] / 60 / 60;
    int minute = disp_ctrl.com_data.i_temp[2] / 60 % 60;
    int second = disp_ctrl.com_data.i_temp[2] % 60;
    // time
    x = 96;
    y -= 3;
    // int const font_width = 9;
    int const num_width = 11;
    int const num_height = 22;
    x = disp_draw_user_image_num_with_digits(x, y, hour, 2, num_width, num_9x22_TAB);
    disp_draw_rect_color(x, y + 9, x + 2, y + 9 + 2, RGB565_COLOR_WHITE);
    disp_draw_rect_color(x, y + 19, x + 2, y + 19 + 2, RGB565_COLOR_WHITE);
    x += 5;
    x = disp_draw_user_image_num_with_digits(x, y, minute, 2, num_width, num_9x22_TAB);
    disp_draw_rect_color(x, y + 9, x + 2, y + 9 + 2, RGB565_COLOR_WHITE);
    disp_draw_rect_color(x, y + 19, x + 2, y + 19 + 2, RGB565_COLOR_WHITE);
    x += 5;
    x = disp_draw_user_image_num_with_digits(x, y, second, 2, num_width, num_9x22_TAB);

    int const unit_interver = 6;
    x = 61;
    y += 43;
    disp_draw_user_image(x, y, bmp_heart_rate_icon);
    x = 96;
    y -= 3;
    x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[4], num_width, num_9x22_TAB);
    disp_draw_rect_color(x, y + num_height / 2, x + num_width, y + num_height / 2 + 1, RGB565_COLOR_WHITE);
    x += num_width;
    x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[3], num_width, num_9x22_TAB);
    x += unit_interver;
    disp_draw_user_image(x, y + num_height - 14, bmp_unit_bpm);

    x = 61;
    y += 43;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_C129_calories_icon);
    x = 96;
    y -= 3;
    x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[5], num_width, num_9x22_TAB);
    x += unit_interver;
    disp_draw_user_image(x, y + num_height - 14, bmp_unit_kcal);
}

void view_sports_result_2_display(void)
{
    int const heart_50_height = 32;
    uint8_t *detail_buffer = (uint8_t *)&disp_ctrl.com_data.i_temp[5];
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = sports_mode_get_sports_avg_heart_rate();
        disp_ctrl.com_data.i_temp[1] = sports_mode_get_sports_minimal_heart_rate();
        disp_ctrl.com_data.i_temp[2] = sports_mode_get_sports_maximum_heart_rate();
        int const start_y = 64 + 102;
        disp_ctrl.com_data.i_temp[3] = sports_mode_get_detail_info((sports_mode_detail_t *)detail_buffer, SPORTS_MODE_DETAIL_CACHE_COUNT);
        // change to y
        for (int i = 0; i < disp_ctrl.com_data.i_temp[3]; i++)
        {
            if (detail_buffer[i] != 0)
            {
                detail_buffer[i] = start_y - (detail_buffer[i] - 50) * heart_50_height / 50;
            }
        }
    }

    int x = 0;
    int y = 64;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_C136_hearttate_0_64);
    x = 85;
    y = 28;
    disp_draw_user_image(x, y, bmp_heart_rate_icon);
    x = 110;
    y = 19;
    disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[0], 15, num_12x29_TAB);

    x = 54;
    y = 175;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_C137_hearttate_big_54_175);
    x = 77;
    y = 175;
    disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[2], 11, num_9x22_TAB);

    x = 131;
    y = 175;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_C138_hearttate_small_131_175);

    x = 154;
    y = 175;
    disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[1], 11, num_9x22_TAB);

    int const start_x = 36;
    for (int i = 0; i < disp_ctrl.com_data.i_temp[3] - 1; i++)
    {
        if (detail_buffer[i] != 0 && detail_buffer[i + 1] != 0)
        {
            disp_draw_hr_line(start_x + i * 2, detail_buffer[i], start_x + (i + 1) * 2, detail_buffer[i + 1]);
        }
    }
}
void view_sports_state_0_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[0] = ble_if_connected();
        disp_ctrl.com_data.i_temp[1] = hr_get_hr_data();
        disp_ctrl.com_data.i_temp[2] = sports_mode_get_sports_duration();
        disp_ctrl.com_data.i_temp[5] = hr_if_band_wear();
        disp_ctrl.com_data.i_temp[6] = UIdatetime.time.hour;
        disp_ctrl.com_data.i_temp[7] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = if_time_hour_system_12();
        if (disp_ctrl.com_data.i_temp[8])
        {
            disp_ctrl.com_data.i_temp[8] += (disp_ctrl.com_data.i_temp[6] >= 12); // 1=am,2=pm
            disp_ctrl.com_data.i_temp[6] %= 12;
            if (disp_ctrl.com_data.i_temp[6] == 0)
            {
                disp_ctrl.com_data.i_temp[6] = 12;
            }
        }
        sports_mode_mode_t mode = sports_mode_get_now_mode();
        switch (mode)
        {
        case sports_mode_mode_run_2:
        case sports_mode_mode_walk:
            disp_ctrl.com_data.i_temp[4] = sports_mode_get_sports_mileage();
            if (system_config_get_km_mi() != 0)
            {
                disp_ctrl.com_data.i_temp[4] *= 0.6213712;
            }
            break;
        case sports_mode_mode_work_out:
        case sports_mode_mode_swing:
        case sports_mode_mode_ride:
        case sports_mode_mode_basketball:
        case sports_mode_mode_football:
            disp_ctrl.com_data.i_temp[4] = sports_mode_get_sports_calorie() / 1000;
            break;
        default:
            return;
        }
        disp_ctrl.auto_refresh_time = 500;
    }
    int x = 0;
    int y = 0;
    if (disp_ctrl.com_data.i_temp[2] <= 0)
    {
        disp_ctrl.auto_refresh_time = 100;
        x = 41;
        y = 56;
        switch (disp_ctrl.com_data.i_temp[2])
        {
        case -3:
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_C111_exercise_3_41_56);
            break;
        case -2:
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_C112_exercise_2_41_56);
            break;
        case -1:
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_C113_exercise_1_41_56);
            disp_ctrl.com_data.i_temp[9] = 1;
            break;
        case 0:
            if (disp_ctrl.com_data.i_temp[9])
            {
                disp_ctrl.com_data.i_temp[9] = 0;
                motor_start(100);
                disp_ctrl.auto_close_delay = DISP_CLOSE_TIME_DEFAULT;
            }
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_C114_exercise_go_41_56);
            break;
        }
        return;
    }
    if (!disp_ctrl.com_data.i_temp[5])
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 66, 64, FILE_IMG_T112_not_wear_66_64);
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 24, 147, FILE_IMG_T113_not_wear_text_24_147);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_AlertNotWear, 50);
        return;
    }

    x = 0;
    y = 0;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_C120_heartrateinterval);

    int const font_width = 12;
    int const num_height = 29;
    int const num_width = 15;
    y = 84;

    if (disp_ctrl.com_data.i_temp[1] == 0)
    {
        if (disp_ctrl.auto_refresh_count % 2)
        {
            x = 69 - 14;
            y += num_height / 2;
            int x2 = x + font_width;
            int y2 = y + 1;
            disp_draw_rect_color(x, y, x2, y2, RGB565_COLOR_WHITE);
            x += num_width;
            x2 = x + font_width;
            disp_draw_rect_color(x, y, x2, y2, RGB565_COLOR_WHITE);
        }
    }
    else
    {
        x = 69;
        int level = get_heart_rate_level(disp_ctrl.com_data.i_temp[1]);
        disp_draw_user_image_num_mid_ignore(x, y, RGB565_COLOR_BLACK, disp_ctrl.com_data.i_temp[1], num_width, num_12x29_TAB);
        int index = FILE_IMG_UI_FILE_ENUM_END;
        switch (level)
        {
        case 1:
            x = 0;
            y = 90;
            index = FILE_IMG_C121_heartrateinterval1_0_90;
            break;
        case 2:
            x = 0;
            y = 17;
            index = FILE_IMG_C122_heartrateinterval2_0_17;
            break;
        case 3:
            x = 63;
            y = 0;
            index = FILE_IMG_C123_heartrateinterval3_63_0;
            break;
        case 4:
            x = 180;
            y = 17;
            index = FILE_IMG_C124_heartrateinterval4_108_17;
            break;
        case 5:
            x = 216;
            y = 91;
            index = FILE_IMG_C125_heartrateinterval5_216_91;
            break;
        }
        if (index != FILE_IMG_UI_FILE_ENUM_END)
        {
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, index);
        }
    }
    x = 62;
    y = 56;
    disp_draw_user_image(x, y, bmp_heart_rate_icon);

    // x = 44;
    // y = 121;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_C134_heart_text_44_121);
    disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_sportHeartRate, 50);

    x = 120;

    for (y = 56; y <= 56 + 78; y++)
    {
        disp_draw_point(x, y, 0x5acb);
    }
    int index = 0;
    localize_string_key_t text_index = (localize_string_key_t)0;
    sports_mode_mode_t mode = sports_mode_get_now_mode();
    switch (mode)
    {
    case sports_mode_mode_run_2:
    case sports_mode_mode_walk:
        index = FILE_IMG_C131_distance_icon; // 2415;
        {
            // text_index = FILE_IMG_C135_distance_text_153_121;
            text_index = localize_string_key_sportDistance;
            int wide = 7 + num_width * (get_num_digit(disp_ctrl.com_data.i_temp[4] / 1000) + 2);
            x = 174 - wide / 2;
            y = 84;
            x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[4] / 1000, num_width, num_12x29_TAB);
            x += 2;
            disp_draw_user_image(x, y + 29 - 5, num_12x29_dot);
            x += 8;
            disp_draw_user_image_num_with_digits(x, y, disp_ctrl.com_data.i_temp[4] % 1000 / 10, 2, num_width, num_12x29_TAB);
        }
        break;
    case sports_mode_mode_work_out:
    case sports_mode_mode_swing:
    case sports_mode_mode_ride:
    case sports_mode_mode_basketball:
    case sports_mode_mode_football:
        // text_index = FILE_IMG_C133_calories_text_153_121;
        text_index = localize_string_key_sportCalories;
        index = FILE_IMG_C129_calories_icon;
        x = 174;
        y = 84;
        disp_draw_user_image_num_mid_ignore(x, y, RGB565_COLOR_BLACK, disp_ctrl.com_data.i_temp[4], num_width, num_12x29_TAB);
        break;
    }
    x = 166;
    y = 56;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, index);

    // x = 153;
    // y = 121;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, text_index);
    disp_show_localize(DISP_LFS_FILE_INDEX_2, text_index, 51);

    if (disp_ctrl.com_data.i_temp[0])
    {
        x = 120 - 12 - 2;
        y = 22;
        disp_draw_user_image_num_with_digits(x, y, disp_ctrl.com_data.i_temp[6], 2, 6, num_6x14_TAB);
        x = 120;
        disp_draw_user_image(x, y, num_6x14_colon);
        x = 120 + 2;
        disp_draw_user_image_num_with_digits(x, y, disp_ctrl.com_data.i_temp[7], 2, 6, num_6x14_TAB);
    }
    else
    {
        x = 109;
        y = 22;
        img_gif_com_t const img_cfg_2 = {
            .gif_count = 4,
            .img[0].x = x,
            .img[0].y = y,
            .img[0].sn = FILE_IMG_C115_exercise_con_1_109_22,
            .img[1].x = x,
            .img[1].y = y,
            .img[1].sn = FILE_IMG_C116_exercise_con_2_109_22,
            .img[2].x = x,
            .img[2].y = y,
            .img[2].sn = FILE_IMG_C117_exercise_con_3_109_22,
            .img[3].x = x,
            .img[3].y = y,
            .img[3].sn = FILE_IMG_C118_exercise_con_4_109_22,
        };
        _disp_show_flash_img_gif_all(&img_cfg_2, DISP_LFS_FILE_INDEX_5);
    }
    int second = disp_ctrl.com_data.i_temp[2] % 60;
    int minute = disp_ctrl.com_data.i_temp[2] / 60 % 60;
    int hour = disp_ctrl.com_data.i_temp[2] / 60 / 60;
    y = 159;
    x = 56;
    disp_draw_user_image(x, y, num_15x37_TAB[hour / 10]);
    x = 74;
    disp_draw_user_image(x, y, num_15x37_TAB[hour % 10]);

    x = 104;
    disp_draw_user_image(x, y, num_15x37_TAB[minute / 10]);
    x = 122;
    disp_draw_user_image(x, y, num_15x37_TAB[minute % 10]);

    x = 154;
    disp_draw_user_image(x, y, num_15x37_TAB[second / 10]);
    x = 172;
    disp_draw_user_image(x, y, num_15x37_TAB[second % 10]);
    x = 74 + 18 + 3;
    y = 173;
    disp_draw_rect_color(x, y, x + 4, y + 5, RGB565_COLOR_WHITE);
    x = 122 + 18 + 3;
    disp_draw_rect_color(x, y, x + 4, y + 5, RGB565_COLOR_WHITE);
    y = 189;
    disp_draw_rect_color(x, y, x + 4, y + 5, RGB565_COLOR_WHITE);
    x = 74 + 18 + 3;
    disp_draw_rect_color(x, y, x + 4, y + 5, RGB565_COLOR_WHITE);
    // x = 142;
    // disp_draw_user_image(x, y, num_N312_second_6x32);
}
void view_sports_display(int view)
{
    if (is_level_1_view(view))
    {
        view_sports_level_1_display(view);
    }
    else
    {
        switch (view)
        {
        case view_sports_base:
            view_sports_base_display();
            break;
        case view_sports_level_1_back:
            view_sports_level_1_back_display();
            break;
        case view_sports_state_0:
            view_sports_state_0_display();
            break;
        case view_sports_result_0:
            view_sports_result_0_display();
            break;
        case view_sports_result_1:
            view_sports_result_1_display();
            break;
        case view_sports_result_2:
            view_sports_result_2_display();
            break;
        case view_sports_error_short:
            view_sports_error_short_display();
            break;
        case view_sports_prompt_end:
            view_sports_prompt_end_display();
        default:
            break;
        }
    }
}
view_event_result_t view_sports_level_1_back_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
        display_show(view_sports_level_1_begin, DISP_MODE_DOWN_MOVE);
        return view_event_result_handled;
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        display_show(view_sports, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_state_0_event(event_param_t const *param)
{
    int duration = sports_mode_get_sports_duration();
    if (duration <= 0)
    {
        switch (param->event)
        {
#ifdef EVENT_PREVIOUS_VIEW
        case event_move_previous_view:
#endif
#ifdef EVENT_LEVEL_UP
        case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
        case event_move_level_down:
#endif
#ifdef EVENT_NEXT_VIEW
        case event_move_next_view:
#endif
#ifdef EVENT_BUTTON_FRONT
        case event_button_front_press:
        case event_button_front_long_press:
#endif
            return view_event_result_handled;
        }
    }
    switch (param->event)
    {
    case BUTTON_EVENT_LONG_PRESS:
    {
        switch (param->source)
        {
//#if defined(R31_BOARD) || defined(R35_BOARD) || defined(SW03_BOARD)
					#ifdef EVENT_LEVEL_UP
        case event_source_button_enter:
            sports_mode_stop();
            return view_event_result_handled;
#endif //!< R31_BOARD
        }
        break;
    }
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
        sports_mode_stop();
        return view_event_result_handled;
#endif
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_error_short_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
    case event_button_front_long_press:
#endif
        display_show(view_sports, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_prompt_end_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
    case event_button_front_long_press:
#endif
        display_show(view_sports_result_0, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_result_0_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
    {
        // display_show(view_sports_result_2, DISP_MODE_UP_MOVE);
        return view_event_result_handled;
    }
#endif //!< #ifdef EVENT_PREVIOUS_VIEW
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
    {
        int new_view = 0;
        switch (sports_mode_get_now_mode())
        {
        case sports_mode_mode_run_2:
        case sports_mode_mode_walk:
            new_view = view_sports_result_1;
            break;
        case sports_mode_mode_work_out:
        case sports_mode_mode_swing:
        case sports_mode_mode_ride:
        case sports_mode_mode_basketball:
        case sports_mode_mode_football:
            new_view = view_sports_result_2;
            break;
        }
        display_show(new_view, DISP_MODE_DOWN_MOVE);
        return view_event_result_handled;
    }
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        display_show(view_sports, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_result_1_event(event_param_t const *param)
{
    switch (param->event)
    {
        // case event_move_next_view:
        //     display_show(view_sports_result_2, DISP_MODE_DOWN_MOVE);
        //     return view_event_result_handled;
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        display_show(view_sports, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_result_2_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
        return view_event_result_handled;
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
        display_show(view_sports_result_0, DISP_MODE_DOWN_MOVE);
        return view_event_result_handled;
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        display_show(view_sports, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_event(int view, event_param_t const *param)
{
    if (is_level_1_view(view))
    {
        return view_sports_level_1_event(view, param);
    }
    else
    {
        switch (view)
        {
        case view_sports_level_1_back:
            return view_sports_level_1_back_event(param);
        case view_sports_base:
            switch (param->event)
            {
#ifdef EVENT_BUTTON_FRONT
            case event_button_front_long_press:
#endif
#ifdef EVENT_LEVEL_DOWN
            case event_move_level_down:
#endif
                display_show(view_sports_level_1_begin, DISP_MODE_DOWN_MOVE);
                return view_event_result_handled;
            }
            break;
        case view_sports_state_0:
            return view_sports_state_0_event(param);
        case view_sports_error_short:
            return view_sports_error_short_event(param);
        case view_sports_prompt_end:
            return view_sports_prompt_end_event(param);
        case view_sports_result_0:
            return view_sports_result_0_event(param);
        case view_sports_result_1:
            return view_sports_result_1_event(param);
        case view_sports_result_2:
            return view_sports_result_2_event(param);
        default:
            break;
        }
    }
    return view_event_result_not_handled;
}
